#include "threadPool.h"

//准备工作
//新员工上班，拿到工作手册（线程池信息）
void *threadFunc(void *arg){
    threadPool_t * pthreadPool = (threadPool_t *)arg;
    while(1){  //工作循环（永远待命）
        // 加锁
        int netfd;
        pthread_mutex_lock(&pthreadPool->mutex);
        while(pthreadPool->taskQueue.queueSize == 0 && pthreadPool->exitFlag == 0){
            pthread_cond_wait(&pthreadPool->cond, &pthreadPool->mutex);  //如果都没有就等待广播 cond_wait会自动释放锁，被唤醒时重新获得锁
                                                                         //用while而不是if：防止虚假唤醒  
        }
        if(pthreadPool->exitFlag == 1){  //检查退出标志
            printf("I am worker. I am going to exit!\n");
            pthread_mutex_unlock(&pthreadPool->mutex);
            pthread_exit(NULL);  //主动终止线程  触发系统 释放系统资源
        }
        netfd = pthreadPool->taskQueue.pFront->netfd;//获取队列队首
        printf("I am worker, I got netfd = %d\n", netfd);
        deQueue(&pthreadPool->taskQueue);
        pthread_mutex_unlock(&pthreadPool->mutex);

        printf("I am worker, do something!\n\n");

        // 先收客户端发来的用户名
        train_t train;
        memset(&train, 0, sizeof(train));
        char userName[1024] = {0};
        int ret = 0;
        ret = recv(netfd, &train.length, sizeof(train.length), MSG_WAITALL);
        if(ret == 0) {
            close(netfd);
            continue;
        }
        ret = recv(netfd, train.data, train.length, MSG_WAITALL);
        if(ret == 0) {
            close(netfd);
            continue;
        }
        memcpy(userName, train.data, train.length);  

        printf("userName connect: %s\n", userName);

        // 有用户文件夹就用, 没有就创建
        if(access(userName, F_OK) == 0) {
            printf("老用户, 请放心使用\n");
        } 
        else{
            printf("新用户， ");
            int ret = mkdir(userName, 0777);
            if(ret == -1) {
                printf("文件空间创建失败!\n");
            }
            printf("已注册, 请使用!\n");
        }
        printf("\n");
        
        // 对每一个客户端输入的命令
        // 先收客户端发来的路径
        // 再收客户端发来的命令
        // 然后根据命令执行相应的操作
        // 最后将操作的结果返回给客户端(返回在功能函数中)
        while(1) {
            char path[1024] = {0};
            ret = recv(netfd, &train.length, sizeof(train.length), MSG_WAITALL);
            if(ret == 0) {
                close(netfd);
                break;
            }
            ret = recv(netfd, train.data, train.length, MSG_WAITALL);
            if(ret == 0) {
                close(netfd);
                break;
            }
            memcpy(path, train.data, train.length);

            char command[1024] = {0};
            ret = recv(netfd,&train.length,sizeof(train.length),MSG_WAITALL);
            if(ret == 0) {
                close(netfd);
                break;
            }
            ret = recv(netfd,train.data,train.length,MSG_WAITALL);
            if(ret == 0) {
                close(netfd);
                break;
            }
            memcpy(command,train.data,train.length);

            LOG(userName, command);

            printf("path: %s\n", path);
            printf("command: %s\n", command);

            dealCommand(command, path, netfd);        		
        }

	
        close(netfd);
    }
}
int makeWorker(threadPool_t *pthreadPool){
    for(int i = 0; i < pthreadPool->tidArr.workerNum; ++i){
        pthread_create(&pthreadPool->tidArr.arr[i],NULL,threadFunc,pthreadPool);
    }
    return 0;
}

